package quq.missq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.adapter.MyContractorAdapter;
import quq.missq.beans.Author;
import quq.missq.beans.ContractorBean;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 8/11/15.
 */
public class MyFansActivity extends BaseActivity implements VolleyTool.HTTPListener {

    private ListView mListView;
    private MyContractorAdapter adapter;
    private ArrayList<Author> authors;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_contractor_activity);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText("我的粉丝");
        mListView = (ListView) findViewById(R.id.listView);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initData() {
        adapter = new MyContractorAdapter(this);
        authors = new ArrayList<>();
        adapter.setData(authors);
        mListView.setAdapter(adapter);
        loadNetWorkData();
    }

    private void initListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(MyFansActivity.this, TaPageActivity.class);
     
                Bundle bundle = new Bundle();
                bundle.putInt("uid", adapter.getItem(position).getId());
                bundle.putString("username", adapter.getItem(position).getNkname());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void loadNetWorkData() {
        Map<String, String> params = new HashMap<>();
        params.put("acc_token", UserTools.getUser(this).getAcc_token());
        params.put("pn", "1");
        params.put("maxResults", "200");
        VolleyTool.get(this, Constants.MY_FANS_URL, params, this, Constants.MY_CONTRACTOR, ContractorBean.class);
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        if (requestCode == Constants.MY_CONTRACTOR) {
            ContractorBean bean = (ContractorBean) t;
            adapter.getData().addAll(bean.getData().getResults());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }
}
